package com.peanut.controller;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.peanut.api.SpecFeign;
import com.peanut.common.Result;
import com.peanut.domain.bo.TabInfoBO;
import com.peanut.domain.po.TabInfoPO;
import com.peanut.domain.po.TabSpecInfoPO;
import com.peanut.dto.TabInfoDTO;
import com.peanut.dto.TabSpecInfoDTO;
import com.peanut.service.TabInfoService;
import com.peanut.service.TabSpecInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("info")
@Api("信息管理Api")
public class InfoController {

    @Resource
    private TabInfoService infoService;

    @Resource
    private SpecFeign specFeign;

    @Resource
    private RestTemplate restTemplate;

    @Resource
    private TabSpecInfoService specInfoService;

    /**
     * 添加信息数据
     * @param dto : 信息实体类中的数据
     * @param specId : 规格ID
     * @param model
     * @return
     */
    @PostMapping
    @ApiOperation("添加信息数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "dto",value = "信息实体类",required = true,paramType ="body"),
            @ApiImplicitParam(name = "specId",value = "规格ID",required = true,paramType ="query")
    })
    public Result addInfo(@RequestBody TabInfoDTO dto,@RequestParam Long specId, Model model){
        try {
            ResponseEntity<Result> forEntity = restTemplate.getForEntity("http://127.0.0.1:8084/spec/getSpec", Result.class);
            if (forEntity.getStatusCodeValue() == 200){
                Result body = forEntity.getBody();
                Integer code = body.getCode();
                if (code.intValue() == 10000){
                    String json = JSONUtil.toJsonStr(body.getData());
                    JSONArray objects = JSONUtil.parseArray(json);
                    List<TabInfoDTO> tabInfoDTOS = JSONUtil.toList(objects, TabInfoDTO.class);
                    if (tabInfoDTOS != null){
                        model.addAttribute("spec",tabInfoDTOS);
                        TabInfoBO bo = new TabInfoBO();
                        BeanUtils.copyProperties(dto,bo);
                        int i = infoService.addInfo(bo,specId);
                        return Result.OK().setData(i);
                    }else{
                        return Result.ERROR();
                    }
                }
            }
            return Result.ERROR();
        }catch (Exception e){
            e.printStackTrace();
            return Result.ERROR();
        }
    }

    /**
     * 查询所有信息
     * @param pageNumber : 当前页面
     * @param pageSize : 每页条数
     * @return
     */
    @PostMapping("findAll")
    @ApiOperation("查询所有信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNumber",value = "当前页面",required = true,paramType ="query"),
            @ApiImplicitParam(name = "pageSize",value = "每页条数",required = true,paramType ="query")
    })
    public Result findAll(@RequestParam(value = "pageNumber",defaultValue = "1")Integer pageNumber,
                          @RequestParam(value = "pageSize",defaultValue = "3")Integer pageSize){
        try {
            Page<TabInfoPO> page = infoService.findAll(pageNumber,pageSize);
            return Result.OK().setData(page);
        }catch (Exception e){
            e.printStackTrace();
            return Result.ERROR();
        }
    }

    /**
     * 修改信息数据
     * @param dto : 信息实体类中的数据
     * @return
     */
    @PutMapping
    @ApiOperation("修改信息数据")
    @ApiImplicitParam(name = "dto",value = "信息实体类",required = true,paramType ="body")
    public Result updateInfo(@RequestBody TabInfoDTO dto,Model model){
        try {
            ResponseEntity<Result> forEntity = restTemplate.getForEntity("http://127.0.0.1:8084/spec/getSpec", Result.class);
            if (forEntity.getStatusCodeValue() == 200){
                Result body = forEntity.getBody();
                Integer code = body.getCode();
                if (code.intValue() == 10000){
                    String json = JSONUtil.toJsonStr(body.getData());
                    JSONArray objects = JSONUtil.parseArray(json);
                    List<TabInfoDTO> tabInfoDTOS = JSONUtil.toList(objects, TabInfoDTO.class);
                    if (tabInfoDTOS != null){
                        model.addAttribute("spec",tabInfoDTOS);
                        TabInfoBO bo = new TabInfoBO();
                        BeanUtils.copyProperties(dto,bo);
                        int i = infoService.updateInfo(bo);
                        return Result.OK().setData(i);
                    }else{
                        return Result.ERROR();
                    }
                }
            }
            return Result.ERROR();
        }catch (Exception e){
            e.printStackTrace();
            return Result.ERROR();
        }
    }

    /**
     * 根据ID删除信息
     * @param infoId : 信息ID
     * @return
     */
    @DeleteMapping("/{infoId}")
    @ApiOperation("根据ID删除信息")
    @ApiImplicitParam(name = "infoId",value = "信息ID",required = true,paramType ="path")
    public Result deleteInfo(@PathVariable Long infoId){
        try {
            int i = infoService.deleteInfo(infoId);
            return Result.OK().setData(i);
        }catch (Exception e){
            e.printStackTrace();
            return Result.ERROR();
        }
    }

    @GetMapping("/{catId}")
    public Result<TabInfoDTO> getCatId(@RequestParam Long catId){
        try {
            List<TabInfoPO> list = infoService.getCatId(catId);
            List<TabInfoDTO> list1 = new ArrayList<>();
            for (TabInfoPO tabInfoPO : list) {
                TabInfoDTO dto = new TabInfoDTO();
                BeanUtils.copyProperties(tabInfoPO,dto);
                list1.add(dto);
            }
            return Result.OK().setData(list1);
        }catch (Exception e){
            e.printStackTrace();
            return Result.ERROR();
        }
    }

    @GetMapping("/selectById")
    public Result<TabSpecInfoDTO> selectById(@RequestParam Long specId){
        try {
            List<TabSpecInfoPO> list = specInfoService.selectById(specId);
            List<TabSpecInfoDTO> list1 = new ArrayList<>();
            for (TabSpecInfoPO specInfoPO : list) {
                TabSpecInfoDTO dto = new TabSpecInfoDTO();
                BeanUtils.copyProperties(specInfoPO,dto);
                list1.add(dto);
            }
            return Result.OK().setData(list1);
        }catch (Exception e){
            e.printStackTrace();
            return Result.ERROR();
        }
    }
}
